﻿using BBCore.Application.ViewModels.Admins;
using BBCore.Core.Models.Admins;
using BBCore.Core.Models.EnumTypes;
using BBCore.Infrastructure.Helps;
using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BBCore.Application.Admins
{
    public class AdminService : IAdminService
    {

        public async Task<UserEntity> user_LoginAsync(LoginVo loginVo)
        {
            var password = MD5Encryption.Encrypt(loginVo.Password!);//密码MD5加密
            var user = await UserEntity.Where(x => x.UserName == loginVo.UserName && x.Password == password).Include(x => x.Role).FirstAsync();
            return user;
        }
        public UserEntity user_Login(string userName)
        {
            var user =  UserEntity.Where(x => x.UserName == userName).First();
            return user;
        }
        public async Task<UserEntity> user_LoginAsync(string userName)
        {
            var user = await UserEntity.Where(x => x.UserName == userName).FirstAsync();
            return user;
        }
        public RoleEntity role_Role(int roldId)
        {
            var role = RoleEntity.Where(x => x.Id == roldId).IncludeMany(x => x.Menus).First();
            return role;
        }
        public IEnumerable<SelectedItem> role_SelectedItems(bool isEmpty)
        {
            var items = RoleEntity.Select.ToList(x => new SelectedItem(x.Id.ToString(), x.RoleName!));
            if (isEmpty)
            {
                items.Insert(0,new SelectedItem("", ""));//首行插入空的一行
                return items;
                
            }
            return items;
        }


        public MenuEntity menu_Getmenu(int menuid)
        {
            var item=MenuEntity.Where(x=>x.Id==menuid).First();
            return item;
        }
        public List<MenuItem> menu_CascadingMenu(List<MenuEntity> menuEntities, int parentId)
        {
            var menuitems = menuEntities.Where(x => x.ParentId == parentId)
                .Select(x => new MenuItem { Text = x.MenuName, Items = menu_CascadingMenu(menuEntities, x.Id), Icon = x.Icon, Url = x.Url }).ToList();
            return menuitems;
        }

        public List<TreeViewItem<int>> menu_TreeCascadingMenu(List<MenuEntity> menuEntities, int parentId)
        {
          var items=  menuEntities.Where(x => x.ParentId == parentId)
            .Select(x => new TreeViewItem<int>(x.Id) { Text = x.MenuName, Items = menu_TreeCascadingMenu(menuEntities, x.Id) })
            .ToList();
            return items;

        }
        public async Task<List<MenuEntity>> menu_MenuEntityListAsync()
        {
           return await MenuEntity.Select.ToListAsync();
        }

        public Task<IEnumerable<TableTreeNode<MenuEntity>>> TreeNodeConverter(IEnumerable<MenuEntity> items)
        {
            // 构造树状数据结构
            var ret = BuildTreeNodes(items, 0);
            return Task.FromResult(ret);

            IEnumerable<TableTreeNode<MenuEntity>> BuildTreeNodes(IEnumerable<MenuEntity> items, int parentId)
            {
                var ret = new List<TableTreeNode<MenuEntity>>();
                ret.AddRange(items.Where(i => i.ParentId == parentId).Select((menu, index) => new TableTreeNode<MenuEntity>(menu)
                {
                    HasChildren = items.Any(x => x.ParentId == menu.Id),
                    Items = BuildTreeNodes(items, menu.Id)
                }));
                return ret;
            }
        }

        public MenuEntity menu_GetmenuIncludeMany(int menuid)
        {
            //IncludeMany 有第二个参数，可以进行二次查询前的修饰工作
            /*
             * 例如:Select<Tag>().IncludeMany(a => a.Songs, then => then.Where(song => song.User == "admin")).ToList();
            */
            return MenuEntity.Where(a => a.Id == menuid).IncludeMany(b => b.Query_Info, then => then.OrderBy(c => c.ORDERBY)).First();
        }

        public bool AuthorizingBlock(string username, string url, string operate)
        {
            var tmp = ButtonPermission.Where(a => a.UserName == username && a.MenuUrl == url).First();
            if (tmp == null)
            {
                return false;
            }
            if (!string.IsNullOrEmpty(tmp.Permissions))
            {
                return tmp.Permissions.Contains(operate);
            }
            else
            {
                return false;
            }
        }

      
    }
}
